

module.exports = function(lab){
    return {
        // 添加或者保存slime_store_brand_skus记录，这个记录了brand_sku根据门店的特化配置，根据sku_id 和 store_id 可以特化定义到一条记录，
        // 因此在这种情况下，
        /**
         * @api {jsonrpc} slime@store.updateBrandSKU(form)=result slime@store.updateBrandSKU
         * @apiDescription 更新品牌与sku关系
         * @apiGroup slime
         * @apiParam (form) {String} sku_id 商品id
         * @apiParam (form) {String} store_id 门店id
         * @apiParam (form) {String} on_sale 是否上架
         * @apiParam (form) {Number} list_price 零售价
         *
         * @apiSuccess {Object} result 品牌和sku关联记录
         */
        updateBrandSKU: function(session, { sku_id, store_id, on_sale, list_price }) {
            if (!session.currentOU) {
                throw @lab.errors.not_login;
            }

            return lab.sqlite.find("slime_stores", {
                "id": store_id,
                "ouid": session.currentOU.id,
            }).then(function(result) {

                if (result.records.length) {

                    lab.sqlite.find("slime_store_brand_skus", {
                        "sku_id": sku_id,
                        "store_id": store_id
                    }).then(function(result) {
                        if (result.records.length) {
                            lab.update("slime_store_brand_skus", result.records[0], { on_sale, list_price }).pipe(this);
                        } else {
                            lab.create("slime_store_brand_skus", {
                                "id": @.uuid(),
                                "sku_id": sku_id,
                                "store_id": store_id,
                                "on_sale": on_sale,
                                "list_price": list_price
                            }).pipe(this)
                        }
                    }).pipe(this)

                } else {
                    throw @lab.errors.no_permission;
                }
            })

        },
        @findBrandSKUByID : function({ouid, store_id, sku_id}){
            return lab.slime.store.@findBrandSKU({
                id : ouid,
            },{
                id : store_id
            },sku_id);
        },
        @findBrandSKU : function(currentOU, currentStore, id){
            var query = "select sku.*, store_sku.id as store_sku_id, store_sku.on_sale as store_on_sale, store_sku.stock as store_stock, store_sku.list_price as store_list_price, store_sku.last_update as store_last_update from slime_brand_skus as sku left join slime_store_brand_skus as store_sku on sku.id = store_sku.sku_id and store_sku.store_id = ?  where sku.ouid = ? and sku.id = ?";
            return lab.sqlite.query(query, currentStore.id,currentOU.id,id).then(function(result){
                if (result.records.length) {

                    var sku = result.records[0];

                    if (sku.store_sku_id) {
                        sku.on_sale = sku.store_on_sale;
                        sku.list_price = sku.store_list_price;
                        sku.stock = sku.store_stock;
                    }

                    sku.list_price = sku.list_price;
                    sku.market_price = sku.market_price;

                    this.next(sku);
                } else {
                    throw @lab.errors.object_not_found;
                }
            })
        }
    }
}